package cn.blackbirds.blocks.layer;

import cn.blackbirds.blocks.Globals;
import cn.blackbirds.blocks.MainActivity;
import cn.blackbirds.blocks.R;

import com.openfeint.api.ui.Dashboard;
import com.wiyun.engine.actions.Action;
import com.wiyun.engine.actions.DelayTime;
import com.wiyun.engine.actions.FiniteTimeAction;
import com.wiyun.engine.actions.Hide;
import com.wiyun.engine.actions.IntervalAction;
import com.wiyun.engine.actions.MoveBy;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.actions.Sequence;
import com.wiyun.engine.actions.Show;
import com.wiyun.engine.actions.Action.Callback;
import com.wiyun.engine.nodes.Director;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Menu;
import com.wiyun.engine.nodes.MenuItemSprite;
import com.wiyun.engine.nodes.Scene;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.nodes.TiledSprite;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.transitions.ColorFadeTransition;
import com.wiyun.engine.transitions.TransitionScene;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.engine.types.WYSize;

public class MenuLayer extends Layer {

	MainActivity mainActiveTmp;
	
	public MenuLayer(MainActivity mainActive) {		
		
		mainActiveTmp = mainActive;
			
		TiledSprite	mTS = TiledSprite.make(Texture2D.makePNG(R.drawable.logobg));		
		mTS.setTileDirection(false, false); // 可以设置平铺的方向, 缺省就是x, y方向都平铺S
		addChild(mTS);				
		mTS.autoRelease();
			
		MenuItemSprite item1 = MenuItemSprite.make(R.drawable.starting, R.drawable.starting_p,
				this, "onStart");
		item1.setPosition(240 , 470);

		MenuItemSprite item2 = MenuItemSprite.make(R.drawable.setting, R.drawable.setting_p,
				this, "onHelp");
		item2.setPosition(240 , 385);

		MenuItemSprite item3 = MenuItemSprite.make(R.drawable.scoring, R.drawable.scoring_p,
				this, "onQuit");
		item3.setPosition(240 , 300);

		Menu menu = Menu.make(item1, item2, item3);
		menu.alignItemsVertically();

		addChild(menu);
		
		drawMovingBlock();
		
		showlogo();

	}

	public void onStart() {
		mainActiveTmp.runOnUiThread(new Runnable() {
			public void run() {			
				Globals.playButton();
				GameScene scene = new GameScene(mainActiveTmp);
				Director.getInstance().pushScene(getTransition(scene));
			}
		});			
	}

	public void onHelp() {
		mainActiveTmp.runOnUiThread(new Runnable() {
			public void run() {		
				Globals.playButton();
				HelperScene scene = new HelperScene();
				Director.getInstance().pushScene(getTransition(scene));
				}
			});	
	}

	public void onQuit() {
		mainActiveTmp.runOnUiThread(new Runnable() {
			public void run() {		
				Globals.playButton();
				Dashboard.openLeaderboard("1046237");
			}
		});		
	}
	
	protected TransitionScene getTransition(Scene scene) {
		return (TransitionScene) ColorFadeTransition.make(1, scene,
				new WYColor3B(0, 0, 0)).autoRelease();
	}

	protected TransitionScene getBackTransition(Scene scene) {
		return (TransitionScene) ColorFadeTransition.make(1, scene,
				new WYColor3B(0, 0, 0)).autoRelease();
	}
	/**
	 * 显示logo
	 */
	private void showlogo(){
		WYSize s = Director.getInstance().getWindowSize();
		Sprite logo = Sprite.make(R.drawable.blackbirds);
        addChild(logo);
        logo.setPosition(s.width / 2, s.height / 2);
        
        IntervalAction seq = (IntervalAction)Sequence.make(
        		(FiniteTimeAction)Show.make().autoRelease(),
        		(FiniteTimeAction)DelayTime.make(3).autoRelease(),
        		(FiniteTimeAction)Hide.make().autoRelease()).autoRelease();
        
        seq.setCallback(
        	new Callback() {
        	@Override
    		public void onStart(int actionPointer) {

    		}

    		@Override
    		public void onStop(int actionPointer) {
    			Globals.onPlayBg();
    		}

			@Override
			public void onUpdate(int arg0, float arg1) {
				// TODO Auto-generated method stub
				
			}
    		});
        
        logo.runAction(seq);
	}
	
	/**
	 * 绘制菜单页面移动方框
	 */
	private void drawMovingBlock(){
		WYSize s = Director.getInstance().getWindowSize();
		
        Sprite sprite = Sprite.make(R.drawable.moving_black);
        addChild(sprite);
        sprite.setPosition(151, 181);
        
        IntervalAction move = (IntervalAction)MoveBy.make((float)0.5, 0, 50).autoRelease();
        IntervalAction back = (IntervalAction)move.reverse().autoRelease();
        IntervalAction seq = (IntervalAction)Sequence.make(move, back).autoRelease();
        Action repeat = (Action)RepeatForever.make(seq).autoRelease();
        sprite.runAction(repeat);    
        
        Sprite spritey = Sprite.make(R.drawable.moving_yellow);
        addChild(spritey);
        spritey.setPosition(430, 114);
        
        IntervalAction movey = (IntervalAction)MoveBy.make((float)1, -100, 0).autoRelease();
        IntervalAction backy = (IntervalAction)movey.reverse().autoRelease();
        IntervalAction seqy = (IntervalAction)Sequence.make(movey, backy).autoRelease();
        Action repeaty = (Action)RepeatForever.make(seqy).autoRelease();
        spritey.runAction(repeaty);    
        
        Sprite spriteCaoping = Sprite.make(R.drawable.menu_caocong);
        addChild(spriteCaoping);
        spriteCaoping.setPosition(s.width/2, 50);
        spriteCaoping.autoRelease();
        
	}
}